package com.decent.controller;

import com.decent.entity.Order;
import com.decent.entity.common.Message;
import com.decent.entity.common.PageResult;
import com.decent.service.OrderService;
import com.decent.util.MessageEnum;
import com.decent.util.MessageUtilEnum;
import com.decent.util.MessageUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author decent
 * @Create 2021/3/31 - 18:02
 * @Description
 */
@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping("bindStore")
    public Message bindStore(Integer storeId, String orderNo) {
        if (storeId <= 0) {
            MessageUtils.buildMsg(MessageEnum.UPDATE_ERROR);
        }
        orderService.bindStore(storeId, orderNo);
        return MessageUtils.buildMsg(MessageEnum.UPDATE_SUCCESS);
    }

    @PostMapping("getOrderInfo")
    public Message getOrderInfo(String orderNo) {
        Order orderInfo = orderService.getOrderInfo(orderNo);
        return MessageUtils.buildMsg(MessageEnum.SELECT_SUCCESS, orderInfo);
    }

    @PostMapping("getPageOrder")
    public Message getPageOrder(@RequestParam(defaultValue = "1") Integer pageNum,
                                @RequestParam(defaultValue = "5") Integer pageSize,
                                String startTime, String endTime) {
        PageInfo<Order> pageOrder = orderService.getPageOrder(pageNum, pageSize, startTime, endTime);
        PageResult pageResult = new PageResult();
        pageResult.setList(pageOrder.getList());
        pageResult.setTotal(pageOrder.getTotal());

        return MessageUtils.buildMsg(pageOrder.getTotal() > 0, MessageUtilEnum.PAGE_ENUM, pageResult);

    }

}
